<template>
  <div>
    <navtop title="编辑地址" path="/address"></navtop>
    <div>
      <van-address-edit
        :area-list="areaList"
        :address-info="bianjidata"
        show-delete
        show-set-default
        show-search-result
        :search-result="searchResult"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete"
      />
    </div>
  </div>
</template>
<script>
import area from "../../../static/data/area";
import navtop from "./navtop";
import { Toast } from "vant";
export default {
  components: {
    navtop,
  },
  created() {
    this.areaList = area.AddressInfo;
  },
  data() {
    return {
      areaList: {},
      searchResult: [],
      addressdata: [],
      bianjiarr: [],
      bianjidata: {},
      routeindexarr: [],
      datainfo: {},

      jsonarr: [],
    };
  },
  mounted() {
    this.onEdit();
  },
  methods: {
    //保存信息
    onSave(content) {
      Toast.success("保存成功");
      //   console.log(content);
      let obj = {
        isDefault: content.isDefault,
        name: content.name,
        tel: content.tel,
        address:
          content.city + " " + content.county + " " + content.addressDetail,
      };
      //   this.bianjidata.push(obj)
      this.datainfo = obj;
      console.log(this.datainfo);
      let routeindex = this.$route.query;
      this.routeindexarr = routeindex;
      console.log(this.routeindexarr.index);
      let json = JSON.parse(localStorage.getItem("addressdata"));
      if (this.datainfo.isDefault) {
        for (let i = 0; i < json.length; i++) {
          json[i].isDefault='';
          if (this.routeindexarr.index == i) {
            json[this.routeindexarr.index] = this.datainfo;
          }
        }
      }

      console.log(this.datainfo);
      localStorage.setItem("addressdata", JSON.stringify(json));
    },

    //删除信息
    onDelete() {
      Toast("delete");
    },
    onEdit() {
      //重地址页面拿过来的index
      let bianjiobj = this.$route.query;
      this.bianjiarr = bianjiobj;
      console.log(this.bianjiarr.index);
      //获取
      this.bianjidata = JSON.parse(localStorage.getItem("addressdata"))[
        this.bianjiarr.index
      ];
      console.log(this.bianjidata);
    },
  },
};
</script>